package com.capgemini.diabeo.service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.capgemini.diabeo.model.CategoryDao;
import com.capgemini.diabeo.model.DaoMaster;
import com.capgemini.diabeo.model.DaoSession;
import com.capgemini.diabeo.model.DishDao;
import com.capgemini.diabeo.model.MenuDao;
import com.capgemini.diabeo.model.MenuDishDao;

public class DatabaseService extends SQLiteOpenHelper {

	private static String DB_PATH = "/data/data/com.capgemini.diabeo/databases/";

	private static String DB_NAME = "diabeo.db";

	private static int DATABASE_VERSION = 1;

	private Context context;

	public DatabaseService(Context context) {
		super(context, DB_NAME, null, DATABASE_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		boolean dbExist = checkDataBase();
		if (dbExist) {
			// do nothing - database already exist
		} else {

			// By calling this method and empty database will be created into
			// the default system path
			// of your application so we are gonna be able to overwrite that
			// database with our database.
			this.getReadableDatabase();
			
		}
	}

	private DaoMaster getDaoMaster() {
		return new DaoMaster(getWritableDatabase());
	}

	private DaoSession getDaoSession() {
		DaoMaster daoMaster = getDaoMaster();
		return daoMaster.newSession();
	}

	public DishDao getDishDao() {
		return getDaoSession().getDishDao();
	}

	public CategoryDao getCategoryDao() {
		return getDaoSession().getCategoryDao();
	}


	public MenuDishDao getMenuDishDao() {
		return getDaoSession().getMenuDishDao();
	}

	public MenuDao getMenuDao() {
		return getDaoSession().getMenuDao();
	}

	public void createTables() {
		DaoMaster.createAllTables(getWritableDatabase(), true);
	}

	/**
	 * Check if the database already exist to avoid re-copying the file each
	 * time you open the application.
	 * 
	 * @return true if it exists, false if it doesn't
	 */
	private boolean checkDataBase() {

		SQLiteDatabase checkDB = null;
		try {
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null,
					SQLiteDatabase.OPEN_READONLY);
		} catch (SQLiteException e) {
			// database doesn't exist yet.
		}

		if (checkDB != null) {

			checkDB.close();

		}

		return checkDB != null ? true : false;
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public void copyDataBase() throws IOException {

		// Open your local db as the input stream
		InputStream myInput = this.context.getAssets().open(DB_NAME);

		// Path to the just created empty db
		String outFileName = DB_PATH + DB_NAME;

		// Open the empty db as the output stream
		OutputStream myOutput = new FileOutputStream(outFileName);

		// transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}

		// Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();

	}

}
